package de.hsowl.tod4a.global.http;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import de.hsowl.tod4a.global.util.ZIPUnit;
import de.hsowl.tod4a.global.variables.GlobalFlags;

/**
 * Represents a HTTP header.
 * 
 * @author Sebastian Witt
 */
public class HttpRequest {

	private final String METHOD = "POST /dbmsg HTTP/1.1";
	private final String HOST = "Host: ";
	private final String AGENT = "User-Agent: Java";
	private final String ACCEPT = "Accept: text/html,application/xml;q=0.9,*/*;q=0.8";
	private final String LANGUAGE = "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3";
	private final String ENCODING = "Accept-Encoding: gzip, deflate";
	private final String CONNECTION = "Connection: keep-alive";
	public String USER = "User: ";
	public String PASSWORD = "Password: ";
	private final String TYPE = "Content-Type: application/x-www-form-urlencoded";
	public final String LENGTH = "Content-Length: ";
	private String CONTENT;
	private final static String linebreak = "\r\n";

	public String getCONTENT() {
		return CONTENT;
	}

	public void setCONTENT(String cONTENT) {
		CONTENT = cONTENT;
	}

	/**
	 * Returns a new request header
	 * 
	 * @param host
	 * @param cont
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public byte[] getHeader(String host, String cont) throws UnsupportedEncodingException {
		byte[] content = ZIPUnit.zip(cont.getBytes(GlobalFlags.ENCODING));

		String LENGTH_ = LENGTH + content.length;
		String HOST_ = HOST + host;

		String head = METHOD + linebreak + HOST_ + linebreak + AGENT + linebreak + ACCEPT + linebreak + LANGUAGE + linebreak
				+ ENCODING + linebreak + CONNECTION + linebreak + USER + linebreak + PASSWORD + linebreak + TYPE + linebreak
				+ LENGTH_ + linebreak + linebreak;
		byte[] head_ = head.getBytes();

		byte[] header = new byte[head_.length + content.length];
		System.arraycopy(head_, 0, header, 0, head_.length);
		System.arraycopy(content, 0, header, head_.length, content.length);
		return header;
	}

	/**
	 * Builds a HttpRequest object from an input source
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static synchronized HttpRequest buildRequestHeader(InputStream is) throws IOException, UnsupportedEncodingException {
		BufferedInputStream bis = new BufferedInputStream(is);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		StringBuffer header = new StringBuffer();

		HttpRequest req = new HttpRequest();

		int byte_ = bis.read();
		baos.write(byte_);
		header.append((char) byte_);

		byte[] b = new byte[4096];
		while (bis.available() > 0) {
			bis.read(b);
			baos.write(b, 0, b.length);
			header.append(new String(b));
		}

		int length = -1;
		length = Integer.parseInt(header.toString().split(req.LENGTH)[1].split(linebreak)[0].trim());
		req.USER = header.toString().split(req.USER)[1].split(linebreak)[0].trim();
		req.PASSWORD = header.toString().split(req.PASSWORD)[1].split(linebreak)[0].trim();

		String split = linebreak + linebreak;
		byte[] buffer = new byte[length];
		System.arraycopy(baos.toByteArray(), header.toString().split(split)[0].length() + split.length(), buffer, 0, length);

		String content = new String(ZIPUnit.unzip(buffer));

		req.setCONTENT(content);
		return req;
	}
}
